package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.TravelinfoDao;
import com.wn.entity.Hotelinfo;

import com.wn.entity.Travelinfo;
import com.wn.service.AreaService;
import com.wn.service.TravelinfoService;
import com.wn.utils.PageParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (Travelinfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:41:20
 */
@Service("travelinfoService")
@Transactional(rollbackFor = Exception.class)
public class TravelinfoServiceImpl implements TravelinfoService {
    @Resource
    private TravelinfoDao travelinfoDao;

    @Resource
    private AreaService areaService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Travelinfo queryById(String id) {


        return this.travelinfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public PageInfo<Travelinfo> queryAllByLimit(PageParam<Travelinfo> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Travelinfo> travelList=travelinfoDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Travelinfo> travelPageInfo = new PageInfo<Travelinfo>(travelList);

            return travelPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Travelinfo> newsinfoList=travelinfoDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Travelinfo> newsinfoPageInfo = new PageInfo<Travelinfo>(newsinfoList);

            return newsinfoPageInfo;

        }
    }

    /**
     * 新增数据
     *
     * @param travelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Travelinfo insert(Travelinfo travelinfo) {
        //生成随机交通方式id
        String travelid= UUID.randomUUID().toString().replaceAll("-","")+"_travel";
        travelinfo.setId(travelid);
        this.travelinfoDao.insert(travelinfo);
        return travelinfo;
    }

    /**
     * 修改数据
     *
     * @param travelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Travelinfo update(Travelinfo travelinfo) {
        this.travelinfoDao.update(travelinfo);
        return this.queryById(travelinfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.travelinfoDao.deleteById(id) > 0;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return 实例对象
     */
    @Override
    public Travelinfo deleteLogical(String id) {
        this.travelinfoDao.deleteLogical(id);
        return this.queryById(id);
    }
}